//
//  XHVideoMoreBtn.m
//  ShowYork
//
//  Created by hxh on 2020/9/4.
//  Copyright © 2020 hxh. All rights reserved.
//

typedef void(^doneBlock)(NSInteger index);
#import "XHVideoMoreView.h"
@interface XHVideoMoreView ()
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UIButton *feedbackBtn;
@property (nonatomic,strong)UIImageView *feedbackImgView;
@property (nonatomic,strong)UIButton *reportBtn;
@property (nonatomic,strong)UIImageView *reportImgView;
@property (nonatomic,strong)UILabel *feedbackLb;
@property (nonatomic,strong)UILabel *reportLb;

@property (nonatomic,strong)doneBlock done;
@end

@implementation XHVideoMoreView

- (instancetype)initWithCompleteBlock:(void (^)(NSInteger))completeBlock{
    if (self = [super init]) {
        [self setUI];
        if (completeBlock) {
            self.done = ^(NSInteger index) {
                completeBlock(index);
            };
        }
    }
    return self;
}
- (void)setUI{
    
    
    CGFloat maxW = kScreenW;
    CGFloat maxH = kScreenH;
    
    if ([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortrait ||
    [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortraitUpsideDown) {
        //当前竖屏
        maxW = kScreenH;
        maxH = kScreenW;
    }
    
 
    self.frame = CGRectMake(0, 0, maxW, maxH);
    
    self.containView = [[UIView alloc]init];
    self.containView.backgroundColor = RGBA(0, 0, 0, 0.7);
    [self addSubview:self.containView];
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.right.equalTo(self).offset(maxW/3);
        make.width.mas_equalTo(maxW/3);
    }];
    
    self.feedbackImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"video_feedback_tag"]];
    [self.containView addSubview:self.feedbackImgView];
    
    [self.feedbackImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.containView);
        make.size.mas_equalTo(CGSizeMake(16, 16));
        make.centerX.equalTo(self.containView).offset(-30);
    }];
    
    self.feedbackLb = [[UILabel alloc]init];
    self.feedbackLb.text = @"视频反馈";
    self.feedbackLb.textColor = [UIColor whiteColor];
    self.feedbackLb.font = font(14);
    [self.containView addSubview:self.feedbackLb];
    
    [self.feedbackLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.feedbackImgView);
        make.left.equalTo(self.feedbackImgView.mas_right).offset(8);
    }];
    
    self.feedbackBtn = [[UIButton alloc]init];
    [self.feedbackBtn addTarget:self action:@selector(feedbackMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.feedbackBtn];
    
    [self.feedbackBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.feedbackImgView);
        make.right.equalTo(self.feedbackLb);
        make.top.equalTo(self.feedbackLb).offset(-5);
        make.bottom.equalTo(self.feedbackLb).offset(5);
    }];
    
    self.reportLb = [[UILabel alloc]init];
    self.reportLb.text = @"投诉";
    self.reportLb.textColor = [UIColor whiteColor];
    self.reportLb.font = font(14);
    [self.containView addSubview:self.reportLb];
    [self.reportLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.containView).offset(-(GK_SAVEAREA_BTM + 16));
        make.bottom.equalTo(self.containView).offset(-32);
        
    }];
    
    self.reportImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"video_report_tag"]];
    [self.containView addSubview:self.reportImgView];
    [self.reportImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.reportLb.mas_left).offset(-8);
        make.centerY.equalTo(self.reportLb);
        make.size.mas_equalTo(CGSizeMake(16, 16));
    }];
    
    self.reportBtn = [[UIButton alloc]init];
    [self.reportBtn addTarget:self action:@selector(reportMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.reportBtn];
    
    [self.reportBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.reportImgView);
        make.right.equalTo(self.reportLb);
        make.top.equalTo(self.reportLb).offset(-5);
        make.bottom.equalTo(self.reportLb).offset(5);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    
    [self addGestureRecognizer:tap];
    
}
- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];

    [self layoutIfNeeded];
    
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
           make.right.equalTo(self);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}

- (void)dismiss{

    CGFloat maxW = kScreenW;
    if ([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortrait ||
    [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationPortraitUpsideDown) {
        //当前竖屏
        maxW = kScreenH;
    }
    
    
    [self layoutIfNeeded];
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(maxW/3);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
    
}
- (void)feedbackMethod{
    [self dismiss];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.done) {
            self.done(1);
        }
    });
}
- (void)reportMethod{
    [self dismiss];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.done) {
            self.done(2);
        }
    });
}
@end
